<?php
require_once('label.php');

class Label extends PDO {
	const TABLE = 'Label';

	const ID = 'idLabel';
	const LANGUE = 'idLangue';
	const ID_TABLE = 'idInTable';
	const NOM_TABLE = 'nomTable';
	const LABEL = 'label';

	private $connection;
	private $stmt_select_by_id;
	private $stmt_select_by_name;

	public function __construct($connection) {
		$this->connection = $connection;
		$this->prepare_statements();
	}

	public static function getTableName() {
		return TABLE;
	}

	public function getBeanById($id) {
		$this->stmt_select_by_id->bindParam(':id', $id, PDO::PARAM_INT);
		$this->stmt_select_by_id->execute();
		return $this->stmt_select_by_id->fetchObject('LabelBean');
	}

	public function getBeanByContext($langue, $nom, $idInTable) {
		$this->stmt_select_by_name->bindParam(':lang', $langue, PDO::PARAM_INT);
		$this->stmt_select_by_name->bindParam(':table', $nom, PDO::PARAM_STR);
		$this->stmt_select_by_name->bindParam(':id', $idInTable, PDO::PARAM_INT);
		$this->stmt_select_by_name->execute();
		return $this->stmt_select_by_name->fetchObject('LabelBean');
	}

	private function prepare_statements() {
		$sql = 'SELECT * '.
			' FROM '.self::TABLE.
			' WHERE '.self::ID.' = :id';
		$this->stmt_select_by_id = $this->connection->prepare($sql);

		$sql = 'SELECT '.self::LABEL.
			' FROM '.self::TABLE.
			' WHERE '.self::NOM_TABLE.' = :table AND '.self::LANGUE.' = :lang AND '.self::ID_TABLE.' = :id';
		$this->stmt_select_by_name = $this->connection->prepare($sql);
	}
}
?>
